package ch02;

import java.util.*;
import java.text.SimpleDateFormat;
//用户类
class user {
    String date;
    double amount;
    String category;
    String note;

    public user(String date, double amount, String category, String note) {
        this.date = date;
        this.amount = amount;
        this.category = category;
        this.note = note;
    }

    @Override
    public String toString() {
        return "日期: " + date + ", 金额: " + amount + ", 类别: " + category + ", 备注: " + note;
    }
}
//系统界面
public class PersonalBillManagementSystem {
    private static List<user> incomes = new ArrayList<>();
    private static List<user> expenses = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) {
        while (true) {
            showMenu();
            int choice = Integer.parseInt(scanner.nextLine());
            switch (choice) {
                case 1:
                    recordIncome();
                    break;
                case 2:
                    recordExpense();
                    break;
                case 3:
                    showAllBills();
                    break;
                case 4:
                    queryBills();
                    break;
                case 5:
                    System.out.println("退出系统。");
                    return;
                default:
                    System.out.println("无效的选项，请重新输入！");
            }
        }
    }

    private static void showMenu() {
        System.out.println("=================================");
        System.out.println("欢迎使用个人账单管理系统");
        System.out.println("=================================");
        System.out.println("请选择操作：");
        System.out.println("1. 记录收入");
        System.out.println("2. 记录支出");
        System.out.println("3. 查看所有账单");
        System.out.println("4. 查询账单");
        System.out.println("5. 退出系统");
        System.out.print("请输入选项序号：");
    }
//计入收入
    private static void recordIncome() {
        System.out.println("请输入收入信息：");
        System.out.print("日期（YYYY-MM-DD）：");
        String date = scanner.nextLine();
        if (!isValidDate(date)) {
            System.out.println("日期格式错误！");
            return;
        }
        System.out.print("金额：");
        double amount = Double.parseDouble(scanner.nextLine());
        if (amount <= 0) {
            System.out.println("金额必须为正数！");
            return;
        }
        System.out.print("类别（如工资、奖金等）：");
        String category = scanner.nextLine();
        System.out.print("备注：");
        String note = scanner.nextLine();
        incomes.add(new user(date, amount, category, note));
        System.out.println("收入已成功记录！");
        System.out.println("按任意键返回主菜单...");
        scanner.nextLine();
    }
    //计入支出
    private static void recordExpense() {
        System.out.println("请输入支出信息：");
        System.out.print("日期（YYYY-MM-DD）：");
        String date = scanner.nextLine();
        if (!isValidDate(date)) {
            System.out.println("日期格式错误！");
            return;
        }
        System.out.print("金额：");
        double amount = Double.parseDouble(scanner.nextLine());
        if (amount <= 0) {
            System.out.println("金额必须为正数！");
            return;
        }
        System.out.print("类别（如餐饮、交通、购物等）：");
        String category = scanner.nextLine();
        System.out.print("备注：");
        String note = scanner.nextLine();
        expenses.add(new user(date, amount, category, note));
        System.out.println("支出已成功记录！");
        System.out.println("按任意键返回主菜单...");
        scanner.nextLine();
    }

    private static void showAllBills() {
        System.out.println("=================================");
        System.out.println("所有收入记录：");
        for (user bill : incomes) {
            System.out.println(bill);
        }
        System.out.println("所有支出记录：");
        for (user bill : expenses) {
            System.out.println(bill);
        }
        System.out.println("按任意键返回主菜单...");
        scanner.nextLine();
    }

    private static void queryBills() {
        System.out.println("请选择查询方式：");
        System.out.println("1. 按日期查询");
        System.out.println("2. 按日期范围查询");
        System.out.println("3. 按类别查询");
        int choice = Integer.parseInt(scanner.nextLine());
        switch (choice) {
            case 1:
                System.out.print("请输入日期（YYYY-MM-DD）：");
                String date = scanner.nextLine();
                if (!isValidDate(date)) {
                    System.out.println("日期格式错误！");
                    return;
                }
                queryByDate(date);
                break;
            case 2:
                System.out.print("请输入开始日期（YYYY-MM-DD）：");
                String startDate = scanner.nextLine();
                System.out.print("请输入结束日期（YYYY-MM-DD）：");
                String endDate = scanner.nextLine();
                if (!isValidDate(startDate) || !isValidDate(endDate)) {
                    System.out.println("日期格式错误！");
                    return;
                }
                queryByDateRange(startDate, endDate);
                break;
            case 3:
                System.out.print("请输入类别：");
                String category = scanner.nextLine();
                queryByCategory(category);
                break;
            default:
                System.out.println("无效的选项！");
        }
    }

    private static void queryByDate(String date) {
        System.out.println("=================================");
        System.out.println("按日期查询结果：");
        for (user bill : incomes) {
            if (bill.date.equals(date)) {
                System.out.println(bill);
            }
        }
        for (user bill : expenses) {
            if (bill.date.equals(date)) {
                System.out.println(bill);
            }
        }
        System.out.println("按任意键返回主菜单...");
        scanner.nextLine();
    }

    private static void queryByDateRange(String startDate, String endDate) {
        System.out.println("=================================");
        System.out.println("按日期范围查询结果：");
        for (user bill : incomes) {
            if (bill.date.compareTo(startDate) >= 0 && bill.date.compareTo(endDate) <= 0) {
                System.out.println(bill);
            }
        }
        for (user bill : expenses) {
            if (bill.date.compareTo(startDate) >= 0 && bill.date.compareTo(endDate) <= 0) {
                System.out.println(bill);
            }
        }
        System.out.println("按任意键返回主菜单...");
        scanner.nextLine();
    }

    private static void queryByCategory(String category) {
        System.out.println("=================================");
        System.out.println("按类别查询结果：");
        for (user bill : incomes) {
            if (bill.category.equalsIgnoreCase(category)) {
                System.out.println(bill);
            }
        }
        for (user bill : expenses) {
            if (bill.category.equalsIgnoreCase(category)) {
                System.out.println(bill);
            }
        }
        System.out.println("按任意键返回主菜单...");
        scanner.nextLine();
    }

    private static boolean isValidDate(String date) {
        try {
            dateFormat.setLenient(false);
            dateFormat.parse(date);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
